/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jpa;

import com.camachosanchez.bean.TiposVehiculo;
import com.camachosanchez.controler.jpa.exceptions.IllegalOrphanException;
import com.camachosanchez.controler.jpa.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.camachosanchez.bean.Vehiculos;
import java.util.ArrayList;
import java.util.List;
import com.camachosanchez.bean.TiposEje;
import com.camachosanchez.util.JPAUtil;

/**
 *
 * @author Rolando
 */
public class TiposVehiculoJpaController implements Serializable {

    public TiposVehiculoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return JPAUtil.getEntityManagerFactory().createEntityManager();
    }

    public void create(TiposVehiculo tiposVehiculo) {
        if (tiposVehiculo.getVehiculosList() == null) {
            tiposVehiculo.setVehiculosList(new ArrayList<Vehiculos>());
        }
        if (tiposVehiculo.getTiposEjeList() == null) {
            tiposVehiculo.setTiposEjeList(new ArrayList<TiposEje>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Vehiculos> attachedVehiculosList = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListVehiculosToAttach : tiposVehiculo.getVehiculosList()) {
                vehiculosListVehiculosToAttach = em.getReference(vehiculosListVehiculosToAttach.getClass(), vehiculosListVehiculosToAttach.getId());
                attachedVehiculosList.add(vehiculosListVehiculosToAttach);
            }
            tiposVehiculo.setVehiculosList(attachedVehiculosList);
            List<TiposEje> attachedTiposEjeList = new ArrayList<TiposEje>();
            for (TiposEje tiposEjeListTiposEjeToAttach : tiposVehiculo.getTiposEjeList()) {
                tiposEjeListTiposEjeToAttach = em.getReference(tiposEjeListTiposEjeToAttach.getClass(), tiposEjeListTiposEjeToAttach.getId());
                attachedTiposEjeList.add(tiposEjeListTiposEjeToAttach);
            }
            tiposVehiculo.setTiposEjeList(attachedTiposEjeList);
            em.persist(tiposVehiculo);
            for (Vehiculos vehiculosListVehiculos : tiposVehiculo.getVehiculosList()) {
                TiposVehiculo oldIdTipoOfVehiculosListVehiculos = vehiculosListVehiculos.getIdTipo();
                vehiculosListVehiculos.setIdTipo(tiposVehiculo);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
                if (oldIdTipoOfVehiculosListVehiculos != null) {
                    oldIdTipoOfVehiculosListVehiculos.getVehiculosList().remove(vehiculosListVehiculos);
                    oldIdTipoOfVehiculosListVehiculos = em.merge(oldIdTipoOfVehiculosListVehiculos);
                }
            }
            for (TiposEje tiposEjeListTiposEje : tiposVehiculo.getTiposEjeList()) {
                TiposVehiculo oldIdTipoVehiculoOfTiposEjeListTiposEje = tiposEjeListTiposEje.getIdTipoVehiculo();
                tiposEjeListTiposEje.setIdTipoVehiculo(tiposVehiculo);
                tiposEjeListTiposEje = em.merge(tiposEjeListTiposEje);
                if (oldIdTipoVehiculoOfTiposEjeListTiposEje != null) {
                    oldIdTipoVehiculoOfTiposEjeListTiposEje.getTiposEjeList().remove(tiposEjeListTiposEje);
                    oldIdTipoVehiculoOfTiposEjeListTiposEje = em.merge(oldIdTipoVehiculoOfTiposEjeListTiposEje);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TiposVehiculo tiposVehiculo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TiposVehiculo persistentTiposVehiculo = em.find(TiposVehiculo.class, tiposVehiculo.getId());
            List<Vehiculos> vehiculosListOld = persistentTiposVehiculo.getVehiculosList();
            List<Vehiculos> vehiculosListNew = tiposVehiculo.getVehiculosList();
            List<TiposEje> tiposEjeListOld = persistentTiposVehiculo.getTiposEjeList();
            List<TiposEje> tiposEjeListNew = tiposVehiculo.getTiposEjeList();
            List<String> illegalOrphanMessages = null;
            for (TiposEje tiposEjeListOldTiposEje : tiposEjeListOld) {
                if (!tiposEjeListNew.contains(tiposEjeListOldTiposEje)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain TiposEje " + tiposEjeListOldTiposEje + " since its idTipoVehiculo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Vehiculos> attachedVehiculosListNew = new ArrayList<Vehiculos>();
            for (Vehiculos vehiculosListNewVehiculosToAttach : vehiculosListNew) {
                vehiculosListNewVehiculosToAttach = em.getReference(vehiculosListNewVehiculosToAttach.getClass(), vehiculosListNewVehiculosToAttach.getId());
                attachedVehiculosListNew.add(vehiculosListNewVehiculosToAttach);
            }
            vehiculosListNew = attachedVehiculosListNew;
            tiposVehiculo.setVehiculosList(vehiculosListNew);
            List<TiposEje> attachedTiposEjeListNew = new ArrayList<TiposEje>();
            for (TiposEje tiposEjeListNewTiposEjeToAttach : tiposEjeListNew) {
                tiposEjeListNewTiposEjeToAttach = em.getReference(tiposEjeListNewTiposEjeToAttach.getClass(), tiposEjeListNewTiposEjeToAttach.getId());
                attachedTiposEjeListNew.add(tiposEjeListNewTiposEjeToAttach);
            }
            tiposEjeListNew = attachedTiposEjeListNew;
            tiposVehiculo.setTiposEjeList(tiposEjeListNew);
            tiposVehiculo = em.merge(tiposVehiculo);
            for (Vehiculos vehiculosListOldVehiculos : vehiculosListOld) {
                if (!vehiculosListNew.contains(vehiculosListOldVehiculos)) {
                    vehiculosListOldVehiculos.setIdTipo(null);
                    vehiculosListOldVehiculos = em.merge(vehiculosListOldVehiculos);
                }
            }
            for (Vehiculos vehiculosListNewVehiculos : vehiculosListNew) {
                if (!vehiculosListOld.contains(vehiculosListNewVehiculos)) {
                    TiposVehiculo oldIdTipoOfVehiculosListNewVehiculos = vehiculosListNewVehiculos.getIdTipo();
                    vehiculosListNewVehiculos.setIdTipo(tiposVehiculo);
                    vehiculosListNewVehiculos = em.merge(vehiculosListNewVehiculos);
                    if (oldIdTipoOfVehiculosListNewVehiculos != null && !oldIdTipoOfVehiculosListNewVehiculos.equals(tiposVehiculo)) {
                        oldIdTipoOfVehiculosListNewVehiculos.getVehiculosList().remove(vehiculosListNewVehiculos);
                        oldIdTipoOfVehiculosListNewVehiculos = em.merge(oldIdTipoOfVehiculosListNewVehiculos);
                    }
                }
            }
            for (TiposEje tiposEjeListNewTiposEje : tiposEjeListNew) {
                if (!tiposEjeListOld.contains(tiposEjeListNewTiposEje)) {
                    TiposVehiculo oldIdTipoVehiculoOfTiposEjeListNewTiposEje = tiposEjeListNewTiposEje.getIdTipoVehiculo();
                    tiposEjeListNewTiposEje.setIdTipoVehiculo(tiposVehiculo);
                    tiposEjeListNewTiposEje = em.merge(tiposEjeListNewTiposEje);
                    if (oldIdTipoVehiculoOfTiposEjeListNewTiposEje != null && !oldIdTipoVehiculoOfTiposEjeListNewTiposEje.equals(tiposVehiculo)) {
                        oldIdTipoVehiculoOfTiposEjeListNewTiposEje.getTiposEjeList().remove(tiposEjeListNewTiposEje);
                        oldIdTipoVehiculoOfTiposEjeListNewTiposEje = em.merge(oldIdTipoVehiculoOfTiposEjeListNewTiposEje);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Short id = tiposVehiculo.getId();
                if (findTiposVehiculo(id) == null) {
                    throw new NonexistentEntityException("The tiposVehiculo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Short id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TiposVehiculo tiposVehiculo;
            try {
                tiposVehiculo = em.getReference(TiposVehiculo.class, id);
                tiposVehiculo.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tiposVehiculo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<TiposEje> tiposEjeListOrphanCheck = tiposVehiculo.getTiposEjeList();
            for (TiposEje tiposEjeListOrphanCheckTiposEje : tiposEjeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TiposVehiculo (" + tiposVehiculo + ") cannot be destroyed since the TiposEje " + tiposEjeListOrphanCheckTiposEje + " in its tiposEjeList field has a non-nullable idTipoVehiculo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Vehiculos> vehiculosList = tiposVehiculo.getVehiculosList();
            for (Vehiculos vehiculosListVehiculos : vehiculosList) {
                vehiculosListVehiculos.setIdTipo(null);
                vehiculosListVehiculos = em.merge(vehiculosListVehiculos);
            }
            em.remove(tiposVehiculo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TiposVehiculo> findTiposVehiculoEntities() {
        return findTiposVehiculoEntities(true, -1, -1);
    }

    public List<TiposVehiculo> findTiposVehiculoEntities(int maxResults, int firstResult) {
        return findTiposVehiculoEntities(false, maxResults, firstResult);
    }

    private List<TiposVehiculo> findTiposVehiculoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TiposVehiculo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TiposVehiculo findTiposVehiculo(Short id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TiposVehiculo.class, id);
        } finally {
            em.close();
        }
    }

    public int getTiposVehiculoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from TiposVehiculo as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
